import Vue from "vue";
import VueRouter from "vue-router";

const Login = () =>
  import(
    /* webpackChunkName: "Login-Home-Welcome" */ "../components/Login.vue"
  );
const Home = () =>
  import(/* webpackChunkName: "Login-Home-Welcome" */ "../components/Home.vue");
const Welcome = () =>
  import(
    /* webpackChunkName: "Login-Home-Welcome" */ "../components/Welcome.vue"
  );

const UserList = () =>
  import(/* webpackChunkName: "User" */ "../components/user/UserList.vue");
const UserEdit = () =>
  import(/* webpackChunkName: "User" */ "../components/user/UserEdit.vue");
const UserAdd = () =>
  import(/* webpackChunkName: "User" */ "../components/user/UserAdd.vue");

const ArticleList = () =>
  import(
    /* webpackChunkName: "Article" */ "../components/article/ArticleList.vue"
  );
const ArticleAdd = () =>
  import(
    /* webpackChunkName: "Article" */ "../components/article/ArticleAdd.vue"
  );
const ArticleEdit = () =>
  import(
    /* webpackChunkName: "Article" */ "../components/article/ArticleEdit.vue"
  );

const ArticleItemList = () =>
  import(
    /* webpackChunkName: "ArticleItem" */ "../components/articleItem/ArticleItemList.vue"
  );
const ArticleItemAdd = () =>
  import(
    /* webpackChunkName: "ArticleItem" */ "../components/articleItem/ArticleItemAdd.vue"
  );
const ArticleItemEdit = () =>
  import(
    /* webpackChunkName: "ArticleItem" */ "../components/articleItem/ArticleItemEdit.vue"
  );

const VideoList = () =>
  import(
    /* webpackChunkName: "ArticleItem" */ "../components/video/VideoList.vue"
  );
const VideoAdd = () =>
  import(
    /* webpackChunkName: "ArticleItem" */ "../components/video/VideoAdd.vue"
  );
const VideoEdit = () =>
  import(
    /* webpackChunkName: "ArticleItem" */ "../components/video/VideoEdit.vue"
  );

const InfoView = () =>
  import(/* webpackChunkName: "Info" */ "../components/information/View.vue");

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      component: Login,
    },
    {
      path: "/home",
      component: Home,
      redirect: "/welcome",
      children: [
        {
          path: "/welcome",
          component: Welcome,
        },
        {
          path: "/user/list",
          component: UserList,
        },
        {
          path: "/user/edit/:id",
          component: UserEdit,
        },
        {
          path: "/user/add",
          component: UserAdd,
        },
        {
          path: "/article/list",
          component: ArticleList,
        },
        {
          path: "/article/add",
          component: ArticleAdd,
        },
        {
          path: "/article/edit/:id",
          component: ArticleEdit,
        },
        {
          path: "/article-item/list",
          component: ArticleItemList,
        },
        {
          path: "/article-item/add",
          component: ArticleItemAdd,
        },
        {
          path: "/article-item/edit/:id",
          component: ArticleItemEdit,
        },
        {
          path: "/video/list",
          component: VideoList,
        },
        {
          path: "/video/add",
          component: VideoAdd,
        },
        {
          path: "/video/edit/:id",
          component: VideoEdit,
        },
        {
          path: "/information/view",
          component: InfoView,
        },
      ],
    },
  ],
});

// 挂在路由导航

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  // 获取token
  const token = window.sessionStorage.getItem("token");
  if (!token) return next("/login");
  next();
});

export default router;
